<template>
  <div class="bottom-bar">
    <div class="check-all">
      <check-button 
        class="button" 
        :isActive="isSelectAll" 
        @click.native="buttonClick">
      </check-button>
      <span>全选</span>
    </div>
    <div class="count">
      <span>合计:</span>
      <span>{{totalPrice}}</span>
      <span>结算{{totalNumber}}</span>
    </div>
  </div>
</template>

<script>
import CheckButton from "@/components/common/checkbutton/CheckButton";

export default {
  components: {
    CheckButton
  },
  computed: {
    totalPrice() {
      return '￥' + this.$store.state.cartList.filter(item => {
        return item.checked === true
      }).reduce((prevalue,item) => {
        return prevalue + item.price * item.count
      },0).toFixed(2)
    },
    totalNumber() {
      return '(' + this.$store.state.cartList.filter(item => {
        return item.checked === true
      }).length + ')'
    },
    isSelectAll() {
      if(this.$store.state.cartList.length === 0)return false;
      return !this.$store.state.cartList.find(item => !item.checked)
    }
  },
  methods: {
    buttonClick() {
      if(this.isSelectAll){
        this.$store.state.cartList.forEach(element => {
          element.checked = false;
        });
      }else{
        this.$store.state.cartList.forEach(element => {
          element.checked = true;
        })
      }
    }
  }
};
</script>

<style scoped>
.bottom-bar {
  position: relative;
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  padding: 0 8px;
}

.check-all {  
  display: flex;
  align-items: center;
}

.button {
  margin-right: 5px;
}

.check-all span {
  font-size: 14px;
  color: #666;
}

.count {
  height: 40px;
  line-height: 40px;
  font-size: 15px;
}

.count span:nth-child(1) {
  color: #000;
}

.count span:nth-child(2) {
  color: red;
  font-size: 13px;
}

.count span:nth-child(3) {
  display: inline-block;
  height: 28px;
  width: 60px;
  text-align: center;
  line-height: 30px;
  background-color: red;
  color: #fff;
  border-radius: 10px;
  margin-left: 5px;
}
</style>